﻿
/**********************************天使物流*******************************************
* 功能描述:基本信息-外协单位
* 作者:李周蓬
* 时间:
* 
**********************************天使物流*******************************************/using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.BaseInfo.Outsources;
using TianShiProject.Models.Common;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.BaseInfo.Outsources
{
    public class OutsourceRepository : DapperDbContextRepository, IOutsourceRepository
    {
        public OutsourceRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 显示外协单位
        /// </summary>
        /// <param name="name"></param>
        /// <param name="phone"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<OutsourceDto>> GetShowOutsource(string name, string phone, int pageindex, int pagesize)
        {
            PageResponse<OutsourceDto> response = new PageResponse<OutsourceDto>();
            var sql = $"select * from Outsource where IsDelete=0 ";
            if (!string.IsNullOrWhiteSpace(phone))
            {
                sql += $" and Phone='{phone}'";
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += $" and OutsourceName like '%" + name + "%'";
            }
            var list = await this.QueryAsync<OutsourceDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<OutsourceDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        /// <summary>
        /// 删除外协单位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteDelOutsource(int id)
        {
            var sql = $"update Outsource set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 批量删除外协单位
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteDelsOutsource(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Outsource set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        /// <summary>
        /// 编辑修改
        /// </summary>
        /// <param name="OutsourceDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateOutsource(OutsourceDto outsourceDto)
        {
            var sql = $"update Outsource set OutsourceName='{outsourceDto.OutsourceName}',Phone='{outsourceDto.Phone}',OutsourceEmail='{outsourceDto.OutsourceEmail}',OutsourceAddr='{outsourceDto.OutsourceAddr}',Temp='{outsourceDto.Temp}',Remark='{outsourceDto.Remark}',CreateTime='{outsourceDto.CreateTime.ToString("yyyy/MM/dd")}' where Id={outsourceDto.Id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 添加外协单位
        /// </summary>
        /// <param name="OutsourceDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddOutsource(OutsourceDto outsourceDto)
        {
            var sql = $"insert into Outsource values('{outsourceDto.OutsourceNumber}','{outsourceDto.OutsourceName}','{outsourceDto.Phone}','{outsourceDto.OutsourceEmail}','{outsourceDto.OutsourceAddr}','{outsourceDto.Temp}','{outsourceDto.Remark}',GETDATE(),0)";
            var list = await ExecuteAsync
               (sql, outsourceDto);
            return list;
        }
    }
}
